package com.siwei.mes.service.laboratory.impl;

import com.siwei.mes.entity.erp.Rwdextra;
import com.siwei.mes.entity.sync.erp.ErpCustomer;
import com.siwei.mes.entity.sync.erp.ErpProject;
import com.siwei.mes.mapper.erp.RwdextraMapper;
import com.siwei.mes.mapper.syncErp.ErpCustomerMapper;
import com.siwei.mes.mapper.syncErp.ErpProjectMapper;
import com.siwei.mes.service.laboratory.ContractInfoService;
import com.siwei.mes.service.laboratory.CustomInfoService;
import com.siwei.mes.service.laboratory.MockSyncDataService;
import com.siwei.mes.sync.erp.service.SyncErpErpTrwdService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

/**
 * 模拟同步所有数据到协会
 *
 * @Description: 模拟同步所有数据到协会
 * @Version: 1.0
 * @author: mlchen
 * @date: 2024/11/4 09:19
 */
@Slf4j
@Service
public class MockSyncDataServiceImpl implements MockSyncDataService {
    @Resource
    private ErpCustomerMapper erpCustomerMapper;
    @Resource
    private CustomInfoService customInfoService;
    @Resource
    private ErpProjectMapper erpProjectMapper;
    @Resource
    private ContractInfoService contractInfoService;
    @Resource
    private RwdextraMapper rwdextraMapper;

    @Resource
    private SyncErpErpTrwdService syncErpErpTrwdService;

    @Override
    public String syncCustomer() {
        int success = 0;
        int fail = 0;
        int index = 1;
        List<ErpCustomer> list = erpCustomerMapper.getList(new ErpCustomer());
        for (ErpCustomer erpCustomer : list) {
            try {
                log.info("【模拟同步客户到协会】第【{}】条开始推送！", index);
                customInfoService.saveOrUpdate(erpCustomer, true, true);
                success++;
            } catch (Exception e) {
                log.error("【模拟同步客户到协会】第【{}】条推送失败，错误原因：【{}】", index, e.getMessage(), e);
                fail++;
            } finally {
                index++;
            }
        }
        return "模拟推送所有客户到协会，总条数：" + list.size() + "成功：" + success + "失败：" + fail;
    }

    @Override
    public String syncProject() {
        int success = 0;
        int fail = 0;
        int index = 1;
        List<ErpProject> list = erpProjectMapper.getList(new ErpProject());
        for (ErpProject erpProject : list) {
            try {
                log.info("【模拟同步工程和合同到协会】第【{}】条开始推送！", index);
                contractInfoService.saveOrUpdate(erpProject, true, true);
                success++;
            } catch (Exception e) {
                log.error("【模拟同步客户到协会】第【{}】条推送失败，错误原因：【{}】", index, e.getMessage(), e);
                fail++;
            } finally {
                index++;
            }
        }
        return "模拟推送所有工程和合同到协会，总条数：" + list.size() + "成功：" + success + "失败：" + fail;
    }

    @Override
    public String syncRwdextra() {
        int success = 0;
        int fail = 0;
        int index = 1;
        List<Rwdextra> list = rwdextraMapper.getList(new Rwdextra());
        Collections.reverse(list);//这里集合反转 为了最早的数据 最先插入
        for (Rwdextra rwdextra : list) {
            try {
                log.info("【模拟同步任务单到协会】第【{}】条开始推送！", index);
                syncErpErpTrwdService.syncLaboratoryInfo(rwdextra, null, true, true,false);
                success++;
            } catch (Exception e) {
                log.error("【模拟同步客户到协会】第【{}】条推送失败，错误原因：【{}】", index, e.getMessage(), e);
                fail++;
            } finally {
                index++;
            }
        }
        return "模拟推送所有任务单到协会，总条数：" + list.size() + "成功：" + success + "失败：" + fail;
    }
}
